package com.note.back.service;

import com.note.back.mapper.CategoryMapper;
import com.note.back.pojo.Category;
import com.note.back.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public void add(Category category) {
        Subject subject = SecurityUtils.getSubject();
        category.setAuthor(((User)subject.getPrincipal()).getId());
        categoryMapper.add(category);
    }

    public List<Category> list() {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = ((User)subject.getPrincipal()).getId();
        return categoryMapper.list(userId);
    }

    public Category findById(Integer id) {
        return categoryMapper.findById(id);
    }

    public void update(Category category) {
        categoryMapper.update(category);
    }

    public void delete(Integer id) {
        categoryMapper.delete(id);
    }

    public Integer findByName(String category) {
        return categoryMapper.findByName(category);
    }
//    @Autowired
//    CategoryDao categoryDao;
//
//    public List<Category> getAll(){
//        return categoryDao.findAll(Sort.by(Sort.Direction.DESC,"id"));
//    }
//
//    public List<Category> getAllByUser(User author){
//        return categoryDao.findByAuthor(author);
//    }
//
//    public Category getById(int id){
//        return categoryDao.findById(id).get();
//    }
//
//    public Category getByName(String name){ return categoryDao.findByName(name);}
//
//    public void deleteById(int id){
//        categoryDao.deleteById(id);
//    }
//
//    public void updateCategory(Category category){
//        categoryDao.save(category);
//    }

}
